package com.wealth.system.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class LoginInterceptor extends HandlerInterceptorAdapter {
	
	/**
	 * 该方法在目标方法调用之前被调用
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		
		//判断Controller方法
		if (handler instanceof HandlerMethod) {
			HttpSession session =request.getSession();
			if(session.getAttribute("memberid")==null){
				String contextPath = request.getContextPath();
				String requestURI = request.getRequestURI().replace(contextPath, "");
				String queryString = request.getQueryString();
				if(queryString!=null){
					response.sendRedirect(contextPath+"/portal?returnURL="+requestURI+"?"+queryString);
				}else{
					response.sendRedirect(contextPath+"/portal?returnURL="+requestURI);
					
				}
			};
		}	
		
		return true;
	}

}
